package com.example.server.impl;

import com.example.dao.dictDetailMapper.DictDetailMapper;
import com.example.dto.dict.DictDetail;
import com.example.dto.dict.DictDetailCriteria;
import com.example.server.DictDetailServer;
import com.example.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "dict")
public class DictDetailServerImpl implements DictDetailServer {
    @Autowired
    private DictDetailMapper dictDetailMapper;

    /**
     * 新增字典详情
     *
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DictDetail resources) {
        dictDetailMapper.creat(resources);
    }

    /**
     * 删除字典详情
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        dictDetailMapper.delete(id);
    }

    /**
     * 修改字典详情
     *
     * @param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DictDetail resources) {
        DictDetail dictDetail = dictDetailMapper.findById(resources.getId());
        ValidationUtil.isNull(dictDetail.getId(), "DictDetail", "id", resources.getId());
        resources.setId(dictDetail.getId());
        dictDetailMapper.save(resources);
    }

    /**
     * 查询所有字典
     *
     * @param criteria
     * @return
     */
    @Override
    public Map<String, Object> findAll(DictDetailCriteria criteria) {
        Map<String, Object> result = new HashMap<>(4);

        Map<String, Object> css = new HashMap<>();
        css.put("createTime", null);
        css.put("updateTime", null);
        css.put("dickName", criteria.getDictName());
        css.put("page", criteria.getPage());
        css.put("size", criteria.getSize());
        if (criteria.getDictName() == null) {
            List<DictDetail> dictes = dictDetailMapper.findAll(css);
            result.put("content", dictes);
            return result;
        }
        List<DictDetail> dicts = dictDetailMapper.findByDictName(criteria.getDictName());
        int counts =dictDetailMapper.findCount(criteria.getDictName());
        result.put("content", dicts);
        result.put("totalElements",counts);
        return result;
    }

    /**
     * 根据字典名称获取字典详情
     *
     * @param name 字典名称
     * @return
     */
    @Override
    public List<DictDetail> getDictByName(String name) {
        List<DictDetail> some = dictDetailMapper.findByDictName(name);
        return some;
    }
}
